<?php
declare(strict_types=1);
namespace App\Dao;

use App\Model\{Wxuser,Wxvisit,Wxsign,Area};
use Hyperf\DbConnection\Db;

class WxuserDao
{
    private $today_time;
    public function __construct()
    {
        $this->today_time = strtotime(date('Y-m-d',time()));
    }

    /**
     * 用户全国地图
     * @Author abell
     * @DateTime 2020-05-26
     * @param array $request
     * @return array
     */
    public function mapInfo($where,$select=['*']){
        return Area::query()->where($where)->select($select)->get()->toArray();
    }

    /**
     * 小程序用户全国画像
     * @Author abell
     * @DateTime 2020-06-04
     * @param string $request
     * @return array
     */
    public function userMap(){
        return  Db::select('SELECT province, COUNT(*) as count FROM pc_wxuser GROUP BY province');
    }

    /**
     * 小程序更新用户信息
     * @Author abell
     * @DateTime 2020-05-26
     * @param string $request
     * @return bool 小程序更新用户信息
     */
    public function updateUserInfo($where,$request){
        return Wxuser::query()->where($where)->update($request);
    }

    /**
     * 小程序新增用户信息
     * @Author abell
     * @DateTime 2020-05-26
     * @param string $request
     * @return bool 小程序新增用户信息
     */
    public function saveUserInfo($request){
        return Wxuser::query()->insertGetId($request);
    }

    /**
     * 用户签到API
     * @Author abell
     * @DateTime 2020-05-26
     * @param string $openid
     * @return bool
     */
    public function addSign($openid){
        return Wxsign::query()->insert(['openid' =>$openid,'add_time'=>time(),'update_time'=>time(),'remark'=>'first']);
    }

    /**
     * 检查用户是否已经签到
     * @Author abell
     * @DateTime 2020-05-26
     * @param string $openid
     * @return bool
     */
    public function hasSign($openid){
        return Wxsign::query()->where(['openid'=>$openid])
            ->where('add_time', '>', $this->today_time)->exists();
    }

    /**
     * 添加用户访问记录
     * @Author abell
     * @DateTime 2020-05-26
     * @param string $openid
     * @return bool
     */
    public function addVisit($openid){
        return Wxvisit::query()->insertGetId(['openid'=>$openid,'type_explain'=>'login2reg','add_time'=>time(),'update_time'=>time()]);
    }

    /**
     * 检查用户是否已经访问了
     * @Author abell
     * @DateTime 2020-05-26
     * @param string $openid
     * @return bool
     */
    public function hasVisit($openid){
        $where = ['openid' => $openid, 'type_explain' => 'login2reg'];
        return Wxvisit::query()->where($where)->where('add_time', '>', $this->today_time)->exists();
    }

    /**
     * 检查是否已经有了访问记录
     * @Author abell
     * @DateTime 2020-05-26
     * @param string $type_explain
     * @return int
     */
    public function addTypeExplain($type_explain,$openid){
        return Wxvisit::query()->insertGetId(['type_explain' => $type_explain,'openid'=>$openid,'add_time'=>time()]);
    }
    /**
     * 检查是否已经有了访问记录
     * @Author abell
     * @DateTime 2020-05-26
     * @param string $type_explain
     * @return int
     */
    public function hasTypeExplain($type_explain,$openid){
        return Wxvisit::query()->where(['type_explain' => $type_explain,'openid'=>$openid])
            ->where('add_time', '>', $this->today_time)->value('id');
    }

    /**
     * 查询多个用户信息
     * @Author abell
     * @DateTime 2020-05-26
     * @param string $openid
     * @return object 通过key查询多个用户信息
     */
    public function getUserByKeys($key,$values=[],$select=['*']){
        return Wxuser::query()->whereIn($key,$values)->select($select)->get();
    }

    /**
     * 查询一个用户信息
     * @Author abell
     * @DateTime 2020-05-26
     * @param string $openid
     * @return object 通过openid查询一个用户信息
     */
    public function getUser($openid='',$select=['*'],$id=0){
        if($id){
            return Wxuser::query()->where(['id' => $id])->first($select);
        }else{
            return Wxuser::query()->where(['openid' => $openid])->first($select);
        }
    }

    /**
     * 通过呢称查询名下所有的用户
     * @Author abell
     * @DateTime 2020-05-26
     * @param string $order 排序方案
     * @param int $pageSize 默认每页显示数量
     * @param int $pageNum  页面数
     * @param string $nickname 搜索呢称
     * @return array 通过呢称查询名下所有的用户
     */
    public function getUsersByOpenids($order,$pageSize,$pageNum,$nickname=null){
        $query = Wxuser::query()->orderByRaw($order);
        if($nickname){
            return $query->whereRaw("from_base64(nickname) LIKE '%{$nickname}%'")
                ->paginate(intval($pageSize),['*'],'page')->toArray();
        }else{
            return $query->paginate(intval($pageSize),['*'],'page')->toArray();
        }
    }

    /**
     * 通过父类呢称查询名下所有的用户
     * @Author abell
     * @DateTime 2020-05-26
     * @param string $pids 查询父类呢称
     * @param string $order 排序方案
     * @param int $pageSize 默认每页显示数量
     * @param int $pageNum  页面数
     * @param string $nickname 搜索呢称
     * @return array
     */
    public function pidGetOpenids($pids,$order,$pageSize,$pageNum,$nickname=null){
        $openids = Wxuser::query()->whereRaw("from_base64(nickname) LIKE '%{$pids}%'")
            ->select('openid')->get()->toArray();
        $openid_arr = array_column($openids,'openid');
        $query = Wxuser::query()->whereIn('pid',$openid_arr)->orderByRaw($order);
        if($nickname){
            return $query->whereRaw("from_base64(nickname) LIKE '%{$nickname}%'")
                ->paginate(intval($pageSize),['*'],'page',$pageNum)
                ->toArray();
        }else{
            return $query->paginate(intval($pageSize),['*'],'page',$pageNum)->toArray();
        }
    }

    /**
     * 查询用户信息
     * @Author abell
     * @DateTime 2020-05-26
     * @param string $openid
     * @return int
     */
    public function getUserCount(){
        return Wxuser::query()->count();
    }

    /**
     * 查询用户是否存在
     * @Author abell
     * @DateTime 2020-05-26
     * @param string $openid
     * @return bool
     */
    public function hasUser($openid){
        return Wxuser::query()->where(['openid' => $openid])->exists();
    }

    /**
     * 查询用户是否添加了小程序
     * @Author abell
     * @DateTime 2020-05-26
     * @param string $openid
     * @return int
     */
    public function hasAdd($openid){
        return Wxuser::query()->where(['openid' => $openid])->value('hasadd');
    }

    /**
     * 添加小程序操作
     * @Author abell
     * @DateTime 2020-05-26
     * @param string $openid
     * @return int
     */
    public function addSmall($openid){
        return Wxuser::query()->where(['openid' => $openid])->update(['update_time' => time(),'hasadd'=>1]);
    }
}
